package org.qh.web.license;

import de.schlichtherle.license.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.prefs.Preferences;

/**
 * License校验类
 */
public class LicenseVerify {
    private static final Logger logger = LogManager.getLogger(LicenseVerify.class);

    /**
     * 安装License证书
     */
    public synchronized void install(LicenseVerifyParam param) {
        LicenseContent result = null;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //1. 安装证书
        try {
            LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));
            licenseManager.uninstall();

            result = licenseManager.install(new File(param.getLicensePath()));

            logger.info("----------------License dat information------------------\n" +
                "Consumer_Name                 :  浅红综合系统\n" +
                "Project_Name                  :  山西芳始文化管理系统 \n" +
                "Product_Name                  :  general-qh-boot3\n" +
                "Edition                       :  Embed\n" +
                "Version                       :  1.0.0\n" +
                "Create_Date                   :  {}\n" +
                "Expiry_Date                   :  {}\n" +
                "Max_Number                    :  -1\n" +
                "License_Type                  :  trial\n" +
                "----------------License dat information------------------",
                format.format(result.getNotBefore()), format.format(result.getNotAfter()));
        } catch (Exception e) {
            logger.error("证书安装失败！", e);
            // 终止服务
            throw new RuntimeException("证书安装失败, 请配置正确的License证书");
        }

    }

    /**
     * 校验License证书
     */
    public boolean verify() {
        LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //2. 校验证书
        try {
            LicenseContent licenseContent = licenseManager.verify();

            logger.info(MessageFormat.format("证书校验通过，证书有效期：{0} - {1}", format.format(licenseContent.getNotBefore()), format.format(licenseContent.getNotAfter())));
            return true;
        } catch (Exception e) {
            logger.error("证书校验失败！", e);
            return false;
        }
    }

    /**
     * 初始化证书生成参数
     */
    private LicenseParam initLicenseParam(LicenseVerifyParam param) {
        Preferences preferences = Preferences.userNodeForPackage(LicenseVerify.class);

        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());

        KeyStoreParam publicStoreParam = new CustomKeyStoreParam(LicenseVerify.class
            , param.getPublicKeysStorePath()
            , param.getPublicAlias()
            , param.getStorePass()
            , null);

        return new DefaultLicenseParam(param.getSubject()
            , preferences
            , publicStoreParam
            , cipherParam);
    }
}
